package com.aipanda.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 咨询类
 * 
 * @param content 内容
 * @param isShow 是否显示
 * @param ip ip地址
 * @param member 会员
 * @param product 产品
 * @param forConsultation
 * @param replyConsultations 回复咨询
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_consultation")
public class Consultation extends BaseEntity {
	private static final long serialVersionUID = -3950317769006303385L;
	private String content;
	private Boolean isShow;
	private String ip;
	private Member member;
	private Product product;
	private Consultation forConsultation;
	private Set<Consultation> replyConsultations = new HashSet<Consultation>();

	@NotEmpty
	@Length(max = 255)
	@Column(nullable = false, updatable = false)
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name="is_show",nullable = false)
	public Boolean getIsShow() {
		return this.isShow;
	}

	public void setIsShow(Boolean isShow) {
		this.isShow = isShow;
	}

	@Column(nullable = false, updatable = false)
	public String getIp() {
		return this.ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="member",updatable = false)
	public Member getMember() {
		return this.member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="product",nullable = false, updatable = false)
	public Product getProduct() {
		return this.product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="for_consultation",updatable = false)
	public Consultation getForConsultation() {
		return this.forConsultation;
	}

	public void setForConsultation(Consultation forConsultation) {
		this.forConsultation = forConsultation;
	}

	@OneToMany(mappedBy = "forConsultation", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE })
	@OrderBy("createDate asc")
	public Set<Consultation> getReplyConsultations() {
		return this.replyConsultations;
	}

	public void setReplyConsultations(Set<Consultation> replyConsultations) {
		this.replyConsultations = replyConsultations;
	}

	@Transient
	public String getPath() {
		if ((getProduct() != null) && (getProduct().getId() != null)){
			return "/consultation/content/" + getProduct().getId() + ".jhtml";
		}
		return null;
	}
}
